/*
 * 
 * This servlet loads the assignments from an entire course.
 * 
 */
package controllers.classes;

import classes.Breadcrumbs;
import classes.HibernateUtil;
import dao.CourseDao;
import dao.FileDao;
import dao.LessonDao;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Course;
import models.Files;
import models.Lesson;

/**
 *
 * @author Achie
 */
public class AssignmentController extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        long courseid = Long.parseLong(request.getParameter("id")); //get the course id from the url

        //find all files (ASSIGNMENTS) from those lessons, put in list, send to next page

        //----------------------------------------------------------------------
        //Find all lessons from course
        //----------------------------------------------------------------------
        HibernateUtil hu = new HibernateUtil();
        LessonDao ld = new LessonDao(hu.openSession());
        List<Lesson> lessons = ld.findAllByCourse(courseid);

        FileDao fd = new FileDao(hu.openSession());
        List<Files> files = fd.findAllFilesInCourse(courseid);

        List<Files> assignments = new ArrayList<Files>();

        for (int i = 0; i < files.size(); i++) {
            if (files.get(i).getType().equals("assignment")) { //if the file is an assignment
                assignments.add(files.get(i));
            }
        }

        request.setAttribute("file", assignments); //set the list of assignments in the request

        CourseDao cdao = new CourseDao(hu.getSessionFactory().openSession());
        Course course = cdao.get(courseid);
        hu.close(); //close connection

        // Breadcrumbs
        Breadcrumbs[] breadcrumbs = {
            new Breadcrumbs("Index", "Home"),
            new Breadcrumbs("Category", "Course catalog"),
            new Breadcrumbs("Course?courseid=" + course.getId(), course.getName()),
            new Breadcrumbs("AssignmentController/view?id=" + course.getId(), "Assignments")
        };
        request.setAttribute("breadcrumbs", breadcrumbs);

        //Redirect with the object to the assignments page of the course
        RequestDispatcher dispatcher = request.getRequestDispatcher("../jsp/assignments.jsp");
        dispatcher.forward(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
